package org.codehaus.mojo.l10n;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.project.MavenProject;

public class L10n {
	private final Map<String, L10nFile> files;
	private final String basename;
	private final MavenProject project;
	private Set<String> keys = new HashSet<String>();
	private String directory;
	private static final Pattern LOCALE_PATTERN = Pattern
			.compile("_[a-zA-Z]{2}[_]?[a-zA-Z]{0,2}");
	private static final Pattern L10N_FILE_PATTERN = Pattern
			.compile("_[a-zA-Z]{2}[_]?[a-zA-Z]{0,2}\\.properties");
	public static final String FILE_EXTENSION = ".properties";

	public static String findBasename(String filename) {
		if (!filename.endsWith(".properties")) {
			return null;
		} else if (!L10N_FILE_PATTERN.matcher(filename).find()) {
			// filename is already basename
			int index = filename.indexOf(".properties");
			String name = filename.substring(0, index);
			return name;
		} else {
			Matcher matcher = L10N_FILE_PATTERN.matcher(filename);
			matcher.find();
			int start = matcher.start();
			String name = null;
			if (start > 0) {
				name = filename.substring(0, start);
			}
			return name;
		}
	}

	public static String findLocale(String filename) {
		Matcher matcher = LOCALE_PATTERN.matcher(filename);
		boolean found = matcher.find();
		if (found) {
			int start = matcher.start() + 1;
			int end = matcher.end();
			return filename.substring(start, end);
		} else {
			return "";
		}
	}

	public void addFile(String locale, L10nFile l10nFile) {
		files.put(locale, l10nFile);
		directory = l10nFile.getSource().getPath();
	}

	public L10n(String basename, MavenProject prj) {
		this.basename = basename;
		this.project = prj;
		files = new HashMap<String, L10nFile>();
	}

	public void calculateKeys() {
		for (L10nFile l10nFile : files.values()) {
			keys.addAll(l10nFile.getProperties().stringPropertyNames());
		}
	}

	public void calculateStatus() {
		calculateKeys();
		// TODO this does not calculate missing keys for missing files
		for (L10nFile l10nFile : files.values()) {
			l10nFile.setKeyCount(l10nFile.getProperties().size());
			l10nFile.setMissingKeyCount(getKeys().size()
					- l10nFile.getProperties().size());
		}
	}

	public String getProperty(String locale, String key) {
		L10nFile l10nFile = files.get(locale);
		if (l10nFile != null) {
			Properties properties = l10nFile.getProperties();
			if (properties != null) {
				return properties.getProperty(key);
			}
		}
		return null;
	}

	public MavenProject getProject() {
		return project;
	}

	public String getBasename() {
		return basename;
	}

	public Map<String, L10nFile> getFiles() {
		return files;
	}

	public Set<String> getKeys() {
		return keys;
	}

	public void setKeys(Set<String> keys) {
		this.keys = keys;
	}

	@Override
	public String toString() {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<l10n basename=\"" + basename + ">");
		stringBuffer.append("\n");
		for (L10nFile l10nFile : files.values()) {
			stringBuffer.append(l10nFile.toString());
			stringBuffer.append("\n");
		}
		stringBuffer.append("</l10n>");
		return stringBuffer.toString();
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public String getDirectory() {
		return directory;
	}
}
